ig.module(
	'game.entities.base.dragme'
)
.requires(
	'impact.entity',
        'game.entities.base.entityBase'
)

.defines(function()
{
    EntityDragme = EntityEntityBase.extend(
    //EntityDragme = ig.Entity.extend(    
    {
        font: new ig.Font( 'media/04b03.font.png' ),
        draging: false,
    
        size: {x:24, y:24},
	collides: ig.Entity.COLLIDES.ACTIVE,
        vel: {x:0, y:0},
	bufferPos: {x:0, y:0},
	resetPos: false,
	
        init: function(x, y, settings)
        {
	    this.parent(x, y, settings);	    
            this.addAnim('Idle', 1, [0]);
        },
	
        update: function()
        {
	    if (this.resetPos == true)
	    {
		this.resetPos = false;
		this.pos = this.bufferPos;		
	    }
	    
            if (ig.input.released('mouse_left'))
            {
		if (this.draging == true)		
		{
		    this.resetPos = true;
		    this.draging = false;
		}		
            }	    
            else if (ig.input.pressed('mouse_left') && this.isMouseOver())
            {
                var entityArray = ig.game.getEntitiesByType(EntityDragme);
                var found = false;
                for (var i = entityArray.length -1; i >= 0; --i)
                {
		    if (entityArray[i].draging == true)
		    {
			found = true;
			break;
		    }
                }
                
                if (found == false)
                {
                    this.draging = true;
		    this.bufferPos.x = this.pos.x;
		    this.bufferPos.y = this.pos.y;
                }
            }
            
            if (this.draging == true)
            {
                this.pos.x += ig.input.mouse.x - this.lastMouseX;
                this.pos.y += ig.input.mouse.y - this.lastMouseY;
		this.vel.x = 0;
		this.vel.y = 0;
            }	    
	    
            this.lastMouseX = ig.input.mouse.x;
            this.lastMouseY = ig.input.mouse.y;   
            this.parent();
        },            
        
        draw: function()
        {
            this.parent();                    
        }
    });
});